//
//  RegisterViewController.m
//  Mecca
//
//  Created by Ryan Enguero on 5/20/14.
//  Copyright (c) 2014 TapDash Heuristics. All rights reserved.
//

#import "RegisterViewController.h"

#import "PostClass.h"

@interface RegisterViewController (){
    
}



@end


@implementation RegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"StaticData" ofType:@"plist"];
    NSDictionary * values=[[NSDictionary alloc] initWithContentsOfFile:plistPath];
    
    NSArray *arrayValues=[[NSArray alloc] initWithArray:[values valueForKey:@"keyarray1"]];
    NSLog(@"arrayValues = %@",arrayValues);
    
    
    _countriesData =[[NSArray alloc] initWithArray:[values valueForKey:@"keyarray1"]];
    
    NSLog(@"Data = %i", [_countriesData count]);
    
    
    [self.navigationController.navigationBar setHidden:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.btnPropCancel.layer.cornerRadius = 8;
    self.btnPropDone.layer.cornerRadius = 8;
    
    
    self.txtCountry.tag = 4;
    self.txtPassword.tag = 5;
    self.txtConfirmPassword.tag = 6;
    

    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (IBAction)submitAction:(id)sender {
    
    if ([self.emailTxt.text isEqualToString:@""] || [self.contactNumberTxt.text isEqualToString:@""] || [self.displayNameTxt.text isEqualToString:@""] || [self.txtCountry.text isEqualToString:@""]) {
        
        UIAlertView *incompleteInputs = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Complete the required field" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
        [incompleteInputs show];
        
        
        
    }else{
        
        if ([self.txtPassword.text isEqualToString:self.txtConfirmPassword.text]) {
            
            //NSLog(@"Match!");
            
            NSLog(@"Email:%@ Contact Number:%@ Display Name:%@ Country:%@ Password:%@",self.emailTxt.text, self.contactNumberTxt.text, self.displayNameTxt.text, self.txtCountry.text, self.txtPassword.text);
            
            self.receivedData = [NSMutableData data]; // Data
            NSURLRequest *request = [[PostClass sharedItems] registerMethodWithEmail:self.emailTxt.text andContactNumber:self.contactNumberTxt.text andDisplayName:self.displayNameTxt.text andCountry:self.txtCountry.text andPassword:self.txtPassword.text];
            _theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
            
            
        }else{
            
            UIAlertView *notMatch = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Your Password Doesnt Match" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            
            [notMatch show];
            
        }
        
    }
    
}

- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)btnTest:(id)sender {
    
    NSLog(@"Image Data = %@",self.imgProfilePicReg.image);
    
}

- (IBAction)btnDonePicker:(id)sender {
    
    
    [self.txtCountry resignFirstResponder];
    
    NSInteger row;
    row = [self.pickerView selectedRowInComponent:0];
    
    if (self.view.frame.size.height == 568) {
    
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                    animations:^{
                        self.scroller.contentOffset = CGPointMake(0, 0);
                        [self.txtCountry setText:[_countriesData objectAtIndex:row]];
        }
        completion:nil
        ];
    
    }else{
        
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                    animations:^{
                        self.scroller.contentOffset = CGPointMake(0, 0);
                        [self.txtCountry setText:[_countriesData objectAtIndex:row]];
        }
        completion:nil
             ];
        }
    

    
    
}

- (IBAction)btnCancelPicker:(id)sender {
    
    [self.txtCountry resignFirstResponder];
    if (self.view.frame.size.height == 568) {
        
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 0);
                             
                         }
                         completion:nil
         ];
        
    }else{
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 0);
                             
                         }
                         completion:nil
         ];
    }

}

#pragma mark - UITextField Delegate

//-(BOOL)textFieldShouldReturn:(UITextField *)textField{
//
//    [textField resignFirstResponder];
//
//    return YES;
//}


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    
    
    if (textField.tag == 4) {
        
        if (self.view.frame.size.height == 568) {
            
            [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                             animations:^{
                                 self.scroller.contentOffset = CGPointMake(0, 115);
                                 [textField setInputView:self.pickerViewContainer];
                             }
                             completion:nil
             ];
        }else{
            
            [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                             animations:^{
                                 self.scroller.contentOffset = CGPointMake(0, 180);
                                 [textField setInputView:self.pickerViewContainer];
                             }
                             completion:nil
             ];
        }
        
        
        
       
    }else{
        
        if (self.view.frame.size.height == 568) {
            
            [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                             animations:^{
                                 self.scroller.contentOffset = CGPointMake(0, 115);
                             }
                             completion:nil
             ];
        }else{
            
            [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                             animations:^{
                                 self.scroller.contentOffset = CGPointMake(0, 180);
                             }
                             completion:nil
             ];
        }
        
    }
    

    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    if (self.view.frame.size.height == 568) {
        
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 0);
                             
                         }
                         completion:nil
         ];
        
    }else{
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 0);
                             
                         }
                         completion:nil
         ];
    }
    
    
    return YES;
}


-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField.tag == 5) {
        if (self.txtPassword.text.length < 6) {
            NSLog(@"sadfhasfhdasiufa");
        }
    }
    
}

#pragma mark - URL Connection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [_receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error data = %@", error);
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Failed" message:@"Please check you internet connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    
    [alertView show];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    
    
    if (connection == self.theConnection) {
        
        [_receivedData appendData:data];
        
        NSLog(@"data parse = %@", _receivedData);
        
    }
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    if (connection == self.theConnection) {
        
        
        if (!(_receivedData == nil)) {
            NSError *error;
            
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:_receivedData options:kNilOptions error:&error];
            
            NSLog(@"Received Data = %@", json);
            
            
            if ([[[json objectForKey:@"Status"] stringValue] isEqualToString:@"0"]) {
                
                UIAlertView *alertError = [[UIAlertView alloc] initWithTitle:@"Error" message:[json objectForKey:@"Message description"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                
                [alertError show];
                
                
            }else{
                
                UIAlertView *msgSuccess = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Please Check your email account to confirm your account" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                
                [msgSuccess show];
                
                
                [self.navigationController popViewControllerAnimated:YES];
            }

            
            
        }
        NSLog(@"Finish");
    }
    
}



#pragma mark - PickerView;


// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_countriesData count];
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _countriesData[row];
}


@end
